package com.intellect.gfproto;

import java.util.concurrent.atomic.AtomicLong;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJBException;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import org.apache.log4j.Logger;


@Stateless(name="HelloWorldBean", mappedName = "ejb/HelloWorldJNDI")
@Local(HelloWorldService.class)
@Remote(HelloWorldService.class)
public class HelloWorldServiceEJB {

	private static Logger log = Logger.getLogger(HelloWorldServiceEJB.class);

	private static AtomicLong _count = new AtomicLong();
	private HelloWorldServiceImpl impl = null;

	public String getTheName() {
//		log.debug("Well hello there: " + impl.getTheName());
//		return null;
		return impl.getTheName();
	}
	
	@PostConstruct
	public void ejbCreate() {
		log.info("ejbCreate : start " + _count.incrementAndGet());
		//
		try {
			impl = new HelloWorldServiceImpl();
		} catch (Exception e) {
			log.error("Exception " + e.getMessage(), e);
			throw new EJBException(e);
		}
		//
		log.info("ejbCreate : end");
	}

	@PreDestroy
	public void ejbRemove() {
		log.info("ejbRemove : start " + _count.decrementAndGet());
		log.info("ejbRemove : end");
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void ping() {
		if (log.isDebugEnabled()) {
			log.debug(">>");
		}
		//
		impl.ping();
		//
		if (log.isDebugEnabled()) {
			log.debug("<< ");
		}
	}
	
}
